import router from '@/router'
import { toDateString } from 'xe-utils'

export function unitToThousand(unit) {
  let thousand = unit / 10000
  if (thousand >= 1) {
    thousand = parseInt(thousand)
    thousand = thousand + '万'
  } else {
    thousand = unit
  }
  return thousand
}

export function secondToMinute(second) {
  let minute = parseInt(second / 60)
  minute = String(minute).padStart(2, '0')
  let sec = parseInt(second % 60)
  sec = String(sec).padStart(2, '0')
  return `${minute}:${sec}`
}

export function millisecondToMinute(milliseconds) {
  let second = milliseconds / 1000
  return secondToMinute(second)
}

export function isMoreThanLength(value, length) {
  if (!value) {
    return ''
  }
  if (value.length > length) {
    return value.slice(0, length) + '...'
  }
  return value
}

export function getViewImgUrl(url) {
  return url
}

export function goBack() {
  return router.go(-1)
}

export function goNext() {
  return router.go(1)
}

/**
 * this: 左边的对象
 * objects: 对象的不定数组
 * @方法描述: 任意对象与objects的粗略比较(==)
 * */
function equals(...objects) {
  for (const object of objects) {
    if (this != object) {
      return false
    }
  }
  return true
}

export function isNotNull(object) {
  return Object.keys(object || {}).length > 0
}

export function toDate_yyyy_m_d_hh_mm(time) {
  return toDateString(time, 'yyyy年M月d日 hh:mm')
}

export function toDate_yyyy_mm_dd(time) {
  return toDateString(time, 'yyyy-MM-dd')
}

export async function sleep(milliseconds) {
  await new Promise((resolve) => {
    setTimeout(() => {
      resolve()
    }, milliseconds)
  })
}

export function toDate_smartDate(time) {
  // 如果小于1天则显示昨天，如果小于7天则显示n天内
  const date = new Date(time)
  const now = new Date()
  const sub = (now - date) / 1000 / (3600 * 24)
  // 在一天内
  if (sub < 1) {
    const hour = parseInt(24 * sub)
    console.log(`昨天${hour}${toDateString(time, 'hh:mm')}`)
    return `昨天${hour}${date.getHours()}:${date.getMinutes()}`
  } else if (sub >= 1 && sub < 3) {
    const day = parseInt(24 * sub)
    console.log(`${day}天内`)
    return `${day}天内`
  } else if (sub >= 3 && sub <= 30) {
    // 月日
    console.log(time, date)
    return toDateString(time, 'MM月dd日 hh:mm')
  } else {
    // 年月日
    return toDate_yyyy_m_d_hh_mm(time)
  }
}

export function getImageColor(url, callback) {
  const img = new Image(400, 400)
  img.src = url
  img.onload = function () {
    var canvas = document.createElement('canvas')
    canvas.width = img.width
    canvas.height = img.height

    var context = canvas.getContext('2d')
    img.crossOrigin = 'Anonymous'
    context.drawImage(img, 0, 0, canvas.width, canvas.height)

    // 获取像素数据
    var data = context.getImageData(0, 0, img.width, img.height).data
    var r = 1,
      g = 1,
      b = 1
    // 取所有像素的平均值
    for (var row = 0; row < img.height; row++) {
      for (var col = 0; col < img.width; col++) {
        // console.log(data[((img.width * row) + col) * 4])
        if (row == 0) {
          r += data[((img.width * row) + col)]
          g += data[((img.width * row) + col) + 1]
          b += data[((img.width * row) + col) + 2]
        } else {
          r += data[((img.width * row) + col) * 4]
          g += data[((img.width * row) + col) * 4 + 1]
          b += data[((img.width * row) + col) * 4 + 2]
        }
      }
    }

    // 求取平均值
    r /= (img.width * img.height)
    g /= (img.width * img.height)
    b /= (img.width * img.height)

    // 将最终的值取整
    r = Math.round(r)
    g = Math.round(g)
    b = Math.round(b)

    callback && callback(`rgb(${r},${g},${b})`)
  }
}

export default {
  install(app) {
    app.config.globalProperties.unitToThousand = unitToThousand
    app.config.globalProperties.secondToMinute = secondToMinute
    /** 毫秒转分钟 */
    app.config.globalProperties.millisecondToMinute = millisecondToMinute

    app.config.globalProperties.isMoreThanLength = isMoreThanLength

    /** 日期过滤器 */
    app.config.globalProperties.toDate_yyyy_m_d_hh_mm = toDate_yyyy_m_d_hh_mm
    app.config.globalProperties.toDate_yyyy_mm_dd = toDate_yyyy_mm_dd
    // app.filter('toDate_smartDate', toDate_smartDate)
    app.config.globalProperties.getViewImgUrl = getViewImgUrl
    /** 判断object是否为空 */
    app.config.globalProperties.isNotNull = isNotNull
    /** 判断object是否相等 */
    // app.config.globalProperties..equals = equals
    /** 成功提示 */
    app.config.globalProperties.$success = (message) => {
      app.config.globalProperties.$notify({
        title: '成功',
        message,
        type: 'success'
      })
    }
    /** 失败提示 */
    app.config.globalProperties.$error = (message) => {
      app.config.globalProperties.$notify({
        title: '失败',
        message,
        type: 'error'
      })
    }
    /** 失败提示 */
    app.config.globalProperties.$warning = (message) => {
      app.config.globalProperties.$notify({
        title: '温馨提示',
        message,
        type: 'warning'
      })
    }
    window.$error = app.config.globalProperties.$error
    window.$success = app.config.globalProperties.$success
    /** 路由回到上一级 */
    app.config.globalProperties.goBack = goBack
    /** 路由回到下一级 */
    app.config.globalProperties.goNext = goNext
    /** 等待多少毫秒 */
    app.config.globalProperties.sleep = sleep

  }
}

